#!/usr/bin/python
'''
This takes 5 files and finds various overlaps.
'''
import sys

# parameters.
human_f = sys.argv[1]
wallaby_f = sys.argv[2]
mouse_f = sys.argv[3]
possum_f = sys.argv[4]
platypus_f = sys.argv[5]

# read data into sets.
def read_data(f):
	# open file.
	fin = open(f, "rb")
	lines = fin.readlines()
	fin.close()
	
	# build set.
	res = set()
	for line in lines:
		tmp = line.strip()
		if tmp.count("rnd") == 0: continue
		if tmp == "": continue
		res.add(tmp)
	return res
		
# read data.
human = read_data(human_f)
wallaby = read_data(wallaby_f)
mouse = read_data(mouse_f)
possum = read_data(possum_f)
platypus = read_data(platypus_f)

# print shared in all.
tmp = human & wallaby & mouse & possum & platypus
print "shared in all", len(tmp)
for t in tmp:
	print t

# print human,mouse,wallaby,possum, not in platypus.
tmp = human & mouse & wallaby & possum
tmp1 = tmp.difference(platypus)
print "shared in human, mouse, possum, wallaby but not platypus", len(tmp1)
for t in tmp1:
	print t
	
# print wallaby, platypus, not in anything else.
tmp = wallaby & platypus
tmp1 = tmp.difference(human, mouse, possum)
print "shared in wallaby, platypus, but not in human, mouse, possum", len(tmp1)
for t in tmp1:
	print t

# print wallaby, human, mouse.
tmp = wallaby & human & mouse
tmp1 = tmp.difference(possum, platypus)
print "shared in wallaby, human, mouse, not in possum, platypus", len(tmp1)
for t in tmp1:
	print t

# print wallaby, possum only.
tmp = wallaby & possum
tmp1 = tmp.difference(mouse, human, platypus)
print "shared in wallaby, possum, not in human, mouse, platypus", len(tmp1)
for t in tmp1:
	print t

# print wallaby.
tmp = wallaby
tmp1 = tmp.difference(mouse, human, platypus, possum)
print "only in wallaby", len(tmp1)
for t in tmp1:
	print t

# all lineages.
tmp1 = human | mouse
tmp2 = possum | wallaby
tmp3 = platypus
tmp4 = tmp1 & tmp2 & tmp3
print "all lineages", len(tmp4)
